package com.jack.database_exp.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jack.database_exp.bean.*;
import com.jack.database_exp.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@SuppressWarnings("all")
@Controller
public class SellController {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    SellMapper sellMapper;
    @Autowired
    SellInfoMapper sellInfoMapper;
    @Autowired
    RedisTemplate<String,String> redisTemplate;
    @Autowired
    WorkerMapper workerMapper;
    @RequestMapping("/sell")
    public String sell(@RequestBody String json){
        String[] split = json.split(",\"t\",");
        json = "";
        for (int i=0;i<split.length-1;i++) {
            System.out.println(split[i]);
            json+=split[i];
            if(i!=split.length-2){
                json+=",";
            }
        }
        json+="]";
        String last = split[split.length-1].substring(0,split[split.length-1].length()-1);
        List<SellMidBean> sellMidBeans = JSONObject.parseArray(json, SellMidBean.class);
        for (SellMidBean sellMidBean : sellMidBeans) {
            System.out.println(sellMidBean);
        }
        for (SellMidBean sellMidBean : sellMidBeans) {
            System.out.println(sellMidBean);
        }
        Remark remark = JSON.parseObject(last, Remark.class);
        System.out.println(remark);
        if(sellMidBeans==null){
            return "/index";
        }

        Integer WNo = workerMapper.queryWorkerByName(remark.getWorker());
        Sell sell = new Sell();
        Date date = new Date();
        System.out.println(remark);
        date.setYear(remark.getYear()-1900);
        date.setMonth(remark.getMonth()-1);
        System.out.println(date.getMonth()-1);
        sell.setSDate(date);
        sell.setWNo(WNo);
        double total = 0;
        for (SellMidBean sellMidBean : sellMidBeans) {
            total+=sellMidBean.getNum()*sellMidBean.getPrice();
        }
        sell.setSTotal(total);
        sellMapper.insertSell(sell);
        Integer SNo = sell.getSNo();
        List<SellInfo> sellInfos = new ArrayList<>();
        for (SellMidBean sellMidBean : sellMidBeans) {
            SellInfo sellInfo = new SellInfo();
            sellInfo.setSNo(SNo);
            sellInfo.setSIPrice(sellMidBean.getPrice());
            sellInfo.setSINum(sellMidBean.getNum());
            sellInfo.setGNo(sellMidBean.getId());
            sellInfos.add(sellInfo);
            goodsMapper.updateGoodsById(-sellMidBean.getNum(),sellMidBean.getId());
            sellInfoMapper.insertInfo(sellInfo);
        }
        String sellInfos_JSON = JSONObject.toJSONString(sellInfos);
        String sell_JSON = JSONObject.toJSONString(sell);
        redisTemplate.boundValueOps("sellInfos_JSON").set(sellInfos_JSON);
        redisTemplate.boundValueOps("sell_JSON").set(sell_JSON);
        return "index";
    }

    @RequestMapping("/showSell")
    public ModelAndView showPlan(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("showSell");
        String sellInfos_JSON = redisTemplate.boundValueOps("sellInfos_JSON").get();
        String sell_JSON = redisTemplate.boundValueOps("sell_JSON").get();
        Sell sell = JSONObject.parseObject(sell_JSON,Sell.class);
        List<SellInfo> sellInfos = JSONObject.parseArray(sellInfos_JSON,SellInfo.class);
        modelAndView.addObject("sell",sell);
        modelAndView.addObject("sellInfos",sellInfos);
        modelAndView.addObject("name",workerMapper.queryWorkerById(sell.getWNo()));
        return modelAndView;
    }
    @RequestMapping("/showSells")
    public ModelAndView showSells(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("showSells");
        List<Sell> sells = sellMapper.querySells();
        List<SellAndInfos> sellAndInfosList = new ArrayList<>();
        for (Sell sell:sells){
            SellAndInfos sellAndInfos = new SellAndInfos();
            sellAndInfos.setSell(sell);
            sellAndInfos.setSellInfoList(sellInfoMapper.querySellInfoBySNo(sell.getSNo()));
            sellAndInfos.setName(workerMapper.queryWorkerById(sell.getWNo()));
            sellAndInfosList.add(sellAndInfos);
        }
        modelAndView.addObject("sellAndInfosList",sellAndInfosList);
        modelAndView.setViewName("showSells");
        return modelAndView;
    }
}
